from django.db import models

class UserInfo(models.Model):
    username = models.CharField(max_length=64)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=255)
    last_login = models.DateTimeField(null=True, blank=True)  #from django.contrib.auth import login 要求的字段
    @property
    def is_authenticated(self):
        return True

class BlogCategory(models.Model):
    name = models.CharField(max_length=200)

class Blog(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    pub_time = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey(BlogCategory, on_delete=models.CASCADE)
    author = models.ForeignKey(UserInfo, on_delete=models.CASCADE)
    class Meta:
        ordering = ['-pub_time']

class BlogComment(models.Model):
    content = models.TextField()
    pub_time = models.DateTimeField(auto_now_add=True)
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE,related_name='comments')
    Commenter = models.ForeignKey(UserInfo, on_delete=models.CASCADE)
    class Meta:
        ordering = ['-pub_time'] #排序是发生在查询结果中，而不是在数据库表中存储数据的方式



